<?php
namespace H5\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
	public function index(){
		$this->display();
	}

	public function aboutus(){
		$this->detailByName('gywm');
	}

	public function manual(){
		$this->detailByName('sybd');
	}

	public function feedback(){
		if(IS_POST){
			// save the data
			$m = M('feedback');
			$data = $m->create();
			$data['createAt'] = time();
			if($data){
				if($m->add($data)){
					//ok
					$this->success('反馈成功~', U(), 3);
				}else{
					$this->error('反馈失败', U(), 3);
				}
			}else{
				$this->error('反馈失败', U(), 3);
			}
		}else{
			$this->display();
		}
	}

	private function getClientInfo($clientid){
		$m = M('Client');
		$result = $m->where(array(
			'id' => $clientid
		))->find();
		if($result){
			return $result;
		}else{
			return array();
		}

	}

	public function active_success(){
		$this->display();
	}

	public function active(){
		$clientid = I('clientid');
		if(!$clientid){
			$this->error('没有提供用户信息，您尚未登录');
			return;
		}
		$m = M('client');
		$condition = array('id' => $clientid);
		if(IS_POST){
			$code = I('code');
			$pid = intval($code) - 2000;
			// check the pid
			$count = $m->where(array('id' => $pid, 'delflag' => 0))->count();

			if($count != 1){
				$this->error('邀请码错误,请核对后重新输入!'.$count);
				return;
			}
			$r = $m->where($condition)->save(array('parentDistributor'=> $pid));
			if($r){
				$this->success('邀请码激活成功',  U('Index/active_success'));
			}else{
				$this->error('邀请码激活失败');
			}
		}else{
			$p = $m->where($condition)->getField('parentDistributor');
			if('-1' != $p){
				$this->redirect('Index/active_success');
				return;
			}
			$this->assign('clientid', $clientid);
			$this->display();
		}		
	}

	public function share(){
		$clientid = I('clientid');
		$this->assign('clientid', intval($clientid) + 2000);
		$this->display();
	}

	public function fanli(){
		$clientid = I('clientid');
		//是否绑定了手机号,或者支付宝账号
		//显示列表
		$account = $this->getClientInfo($clientid);
		$map = array(
			'client' => $clientid,
			'delflag' => 0,
		);
		//申请的信息
		$m = M('FanliApply');
		$list = $m->where($map)->select();
		$this->assign('list' , $list);
		$total = $m->where(array(
			'client' => $clientid,
			'delflag' => 0,
			'status' => 'DEAL',
		))->sum('commissionAmount');
		$this->assign('account', $account);
		$this->assign('total', $total);
		$this->assign('clientid', $clientid);
        $this->display();
	}

	public function withdrawal_apply(){
		$clientid = intval(I('clientid'));
		$this->assign('clientid', $clientid);
		$account = $this->getClientInfo($clientid);
		$map = array(
			'client' => $clientid,
			'delflag' => 0,
		);
		$this->assign('account', $account);
		if(IS_POST){
			$amount = intval(I('amount'));
			$alipay = I('alipay');
			if($amount < 10){
				$this->error('提现金额不得小于10元');
				return;
			}
			if(!$alipay){
				$this->error('请输入您要提现的支付宝账户');
				return;
			}
			if($amount*100 > $account['balance']){
				$this->error('提现金额超出了您的余额,无法申请');
				return;
			}
			$now = time();
			$data = array(
				'createAt' => $now,
				'updateAt' => $now,
				'applyAt' => $now,
				'status' => 'WAITING',
				'clientid' => $clientid,
				'amount' => $amount,
				'dealAt' => 0,
				'alipay' => $alipay
			);
			$m = M('withdrawal');
			$result = $m->data($data)->add();
			if($m->data($data)->add()){
				$this->success('申请提交成功', U('Index/fanli', array('clientid' => $clientid)), 3);
			}else{
				$this->error('提交失败,请核查您提交的信息', U('Index/withdrawal_apply', array('clientid' => $clientid)), 3);
			}
		}else{
			$this->display();
		}
	}

	public function fanli_apply(){
		$clientid = intval(I('clientid'));
		$this->assign('clientid', $clientid);
		if(IS_POST){
			// get the orders
			$oid1 = I('oid1');
			$oid2 = I('oid2');
			$oid3 = I('oid3');
			if(!($oid1 || $oid2 || $oid3)){
				$this->error('请填写正确的订单号', U('Index/fanli_apply', array('clientid' => $clientid)), 5);
			}else{
				$now = time();
				$list = array();
				foreach(array($oid1, $oid2, $oid3) as $value){
					if($value){
						$data = array(
							'createAt' => $now,
							'updateAt' => $now,
							'applyAt' => $now,
							'status' => 'WAITING',
							'client' => strval($clientid),
							'orders' => $value,
							'dealAt' => 0,
						);
						array_push($list, $data);
					}
				}
				$m = M('FanliApply');
				if($m->addAll($list)){
					$this->success('申请已提交', U('Index/fanli', array('clientid' => $clientid)), 3);
				}else{
					$this->error('提交失败! 请检查您的订单号是否正确', U('Index/fanli_apply', array('clientid' => $clientid)), 3);
				}
			}
		}else{
			$this->display();
		}

  }

	public function charts(){
			$this->display();
	}

		/* 文档模型详情页 */
		//taoshenggou_web/index.php?s=/h5/index/detailByName/name/sybd.html
	public function detailByName($name = ''){
		/* 获取详细信息 */
		$Document = D('Document');
    $info = $Document->detailByName($name);
		$this->showDetail($info, $Document);
  }


    /* 文档模型详情页 */
	public function detail($id = 0){
		/* 标识正确性检测 */
		if(!($id && is_numeric($id))){
			$this->error('文档ID错误！');
		}
		/* 获取详细信息 */
		$Document = D('Document');
    $info = $Document->detail($id);
		$this->showDetail($info, $Document);
  }

  private function showDetail($info, $Document){
    if(!$info){
			$this->error($Document->getError());
		}
		/* 分类信息 */
    $category = $this->category($info['category_id']);

		/* 获取模板 */
		if(!empty($info['template'])){//已定制模板
			$tmpl = $info['template'];
		} elseif (!empty($category['template_detail'])){ //分类已定制模板
			$tmpl = $category['template_detail'];
		} else { //使用默认模板
			$tmpl = 'Post/detail';
    }

		/* 更新浏览数 */
		$map = array('id' => $id);
		$Document->where($map)->setInc('view');

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('info', $info);
		$this->assign('page', $p); //页码
		$this->display($tmpl);
  }

    /* 文档分类检测 */
	private function category($id = 0){
		/* 标识正确性检测 */
		$id = $id ? $id : I('get.category', 0);
		if(empty($id)){
			$this->error('没有指定文档分类！');
		}

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		if($category && 1 == $category['status']){
			switch ($category['display']) {
				case 0:
					$this->error('该分类禁止显示！');
					break;
				//TODO: 更多分类显示状态判断
				default:
					return $category;
			}
		} else {
			$this->error('分类不存在或被禁用！');
		}
	}
}